package cn.ithers.blog.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ithers.blog.dao.LoopDao;
import cn.ithers.blog.dto.LooperDTO;
import cn.ithers.blog.pojo.Looper;
import cn.ithers.blog.pojo.User;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.ILoopService;
import cn.ithers.blog.service.IUserService;
import cn.ithers.blog.utils.Constants;
import cn.ithers.blog.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhanghuan
 * @date: 2020/10/4 12:35
 * @description:
 */
@Slf4j
@Service
public class LoopServiceImpl extends BaseService implements ILoopService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private LoopDao loopDao;

    @Autowired
    private IUserService userService;

    @Override
    public ResponseResult addLoop(LooperDTO looperDTO) {
        //检查数据
        String title = looperDTO.getTitle();
        if (StrUtil.isEmpty(title)) {
            return ResponseResult.FAILED("标题不可为空");
        }
        String imageUrl = looperDTO.getImageUrl();
        if (StrUtil.isEmpty(imageUrl)) {
            return ResponseResult.FAILED("图片url不可为空");
        }
        String targetUrl = looperDTO.getTargetUrl();
        if (StrUtil.isEmpty(targetUrl)) {
            return ResponseResult.FAILED("跳转链接不可为空");
        }
        //补充数据
        Looper looper = new Looper();
        looper.setState(title);
        looper.setImageUrl(imageUrl);
        looper.setTargetUrl(targetUrl);
        looper.setId(idWorker.nextId() + "");
        looper.setCreateTime(new Date());
        looper.setUpdateTime(new Date());

        //保存数据
        loopDao.save(looper);

        return ResponseResult.SUCCESS("添加成功");
    }

    /**
     * 删除轮播图
     *
     * @param loopId
     * @return
     */
    @Override
    public ResponseResult deleteLoop(String loopId) {
        loopDao.deleteById(loopId);
        return ResponseResult.SUCCESS("删除成功");
    }

    /**
     * 修改轮播图
     *
     * @param loopId
     * @param looperDTO
     * @return
     */
    @Override
    public ResponseResult updateLoop(String loopId, LooperDTO looperDTO) {
        //找出来
        Looper looperFromDb = loopDao.findOneById(loopId);
        if (looperFromDb == null) {
            return ResponseResult.FAILED("轮播图不存在");
        }
        //不可以为空的要判空
        String title = looperDTO.getTitle();
        String imageUrl = looperDTO.getImageUrl();
        String targetUrl = looperDTO.getTargetUrl();

        if (!StrUtil.isEmpty(title)) {
            looperFromDb.setTitle(title);
        }

        if (!StrUtil.isEmpty(imageUrl)) {
            looperFromDb.setImageUrl(imageUrl);
        }


        if (!StrUtil.isEmpty(targetUrl)) {
            looperFromDb.setTargetUrl(targetUrl);
        }
        if (!StrUtil.isEmpty(looperDTO.getState())) {
            looperFromDb.setState(looperDTO.getState());
        }
        looperFromDb.setUpdateTime(new Date());
        looperFromDb.setOrder(looperDTO.getOrder());


        //可以为空的直接设置

        //保存数据
        loopDao.save(looperFromDb);
        return ResponseResult.SUCCESS("轮播图更新成功");
    }

    /**
     * 获取轮播图
     *
     * @param loopId
     * @return
     */
    @Override
    public ResponseResult getLoop(String loopId) {
        Looper looper = loopDao.findOneById(loopId);
        if (looper == null) {
            return ResponseResult.FAILED("轮播图不存在");
        }
        return ResponseResult.SUCCESS("轮播图获取成功").setData(looper);
    }

    /**
     * 获取轮播图列表
     *
     * @return
     */
    @Override
    public ResponseResult loopsList() {

        Sort sort =  Sort.by(Sort.Direction.DESC, "order", "createTime");
        User user = userService.checkUser();
        List<Looper> all;
        if (user == null || !Constants.User.ROLE_ADMIN.equals(user.getRoles())) {
            all = loopDao.listLoopByState("1");
        } else {
            all = loopDao.findAll(sort);
        }

        return ResponseResult.SUCCESS("获取轮播图列表成功").setData(all);
    }
}
